<template>
  <div class="music-list">
    <!-- 返回按钮 -->
    <div @click="goBack" class="back">
      <i class="icon-back"></i>
    </div>
    <!-- 标题 -->
    <h1 class="title" v-html="title"></h1>
    <!-- 背景图片 -->
    <div class="bg-image" ref="bgImage" :style="bgStyle">
      <div class="play-wrapper">
        <div class="play" v-show="songs.length > 0" ref="playBtn">
          <i class="icon-play"></i>
          <span class="text">随机播放全部</span>
        </div>
      </div>
      <div class="filter" ref="filter"></div>
    </div>
    <div class="bg-layer" ref="layer"></div>
    <!-- 歌手的歌曲列表 -->
    <scroll @scroll="scroll" :probe-type="probeType" :listen-scroll="listenScroll" class="list" ref="list" :data="songs">
      <div class="song-list-wrapper">
        <song-list @select="selectSong" :songs="songs" />
      </div>
      <div class="loading-container" v-show="!songs.length">
        <loading></loading>
      </div>
    </scroll>
  </div>
</template>

<script>
import SongList from "base/song-list/song-list";
import Scroll from "base/scroll/scroll";
import {prefixStyle} from 'common/js/dom'
import Loading from 'base/loading/loading'
import {mapActions} from 'vuex'

const transform = prefixStyle('transform')
const backdrop = prefixStyle('backdrop-filter')

const RESERVED_HEIGHT = 40;

export default {
  components: {
    SongList,
    Scroll,
    Loading
  },
  data() {
    return {
      scrollY: 0
    };
  },
  props: {
    title: {
      type: String,
      default: ""
    },
    songs: {
      type: Array,
      default: () => {
        return [];
      }
    },
    bgImage: {
      type: String,
      default: ""
    }
  },
  created() {
    this.probeType = 3;
    this.listenScroll = true;
  },
  computed: {
    bgStyle() {
      return `background-image:url(${this.bgImage})`;
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    //监听滚动
    scroll(pos) {
      this.scrollY = pos.y;
    },
    //选择歌曲啦
    selectSong(song,index){
      this.playSong({
        list:this.songs,
        index
      })
    },
    ...mapActions([
      'playSong'
    ])
  },
  mounted() {
    // 图片高度
    this.clientHeight = this.$refs.bgImage.clientHeight;
    // 最大滚动距离
    this.minTransformY = -this.clientHeight + RESERVED_HEIGHT;
    // scroll的top值
    this.$refs.list.$el.style.top = `${this.clientHeight}px`;
  },
  watch: {
    scrollY: function(newY) {
      // 计算最大滚动距离，不能超过图片高度 - RESERVED_HEIGHT
      const transformY = Math.max(this.minTransformY, newY);
      // this.$refs.layer.style.transform = `translate3d(0,${transformY}px,0)`;
      this.$refs.layer.style[transform] = `translate3d(0,${transformY}px,0)`;

      // 判断滚动的距离是否超出了允许的最大滚动距离
      let zIndex = 0;

      // 设置缩放
      let scale = 1
      let blur = 0
      const percent = Math.abs(newY / this.clientHeight)
      if(newY > 0){
        zIndex = 10;
        scale = percent + 1
      } else {
        blur = Math.min(20 * percent, 20)
      }
      // this.$refs.bgImage.style.transform = `scale(${scale})`
      this.$refs.bgImage.style[transform] = `scale(${scale})`

      //设置模糊效果，只有iOS有效
      this.$refs.filter.style[backdrop] = `blur(${blur}px)`
      // this.$refs.filter.style['backdrop-filter'] = `blur(${blur}px)`
			// this.$refs.filter.style['webkitBackdrop-filter'] = `blur(${blur}px)`

      // 判断滚动的距离是否超出了允许的最大滚动距离
      if (newY < transformY) {
        zIndex = 10;
        this.$refs.bgImage.style.height = `${RESERVED_HEIGHT}px`;
        this.$refs.bgImage.style.paddingTop = 0;
        this.$refs.playBtn.style.display = 'none'
      } else {
        this.$refs.bgImage.style.paddingTop = "70%";
        this.$refs.bgImage.style.height = 0;
        this.$refs.playBtn.style.display = ''
      }

      // 设置图片的层级
      this.$refs.bgImage.style.zIndex = zIndex;
    }
  }
};
</script>

<style lang="stylus" scoped>
@import '~common/stylus/variable';

.music-list {
  z-index: 50;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  position: fixed;

  .back {
    position: absolute;
    left: 10px;
    top: 10px;
    color: $color-theme;
    z-index: 60;

    .icon-back {
      font-size: $font-size-large-x;
    }
  }

  .title {
    // text-align: center;
    // position: absolute;
    // top: 10px;
    // width: 100%;
    // left: 0;
    // font-size: $font-size-large;
    // z-index: 55;
    position: absolute;
    top: 0;
    left: 10%;
    z-index: 40;
    width: 80%;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    text-align: center;
    line-height: 40px;
    font-size: 18px;
    color: #fff;
  }

  .bg-image {
    width: 100%;
    height: 0;
    padding-top: 70%;
    transform-origin: top;
    background-size: cover;
    position: relative;

    .play-wrapper{
      position absolute
      width 100%
      bottom 20px
      z-index 60
      .play{
        box-sizing border-box
        width 135px
        padding 7px 0px
        margin 0 auto
        text-align center
        border 1px solid $color-theme
        color $color-theme
        border-radius 100px
        .icon-play{
          display inline-block
          vertical-align middle
          margin-right 6px
          font-size $font-size-medium-x
        }
        .text{
          display inline-block
          vertical-align middle
          font-size $font-size-small
        }
      }
    }

    .filter {
      right: 0;
      bottom: 0;
      position: absolute;
      left: 0;
      top: 0;
      background-color: rgba(7, 17, 27, 0.4);
    }
  }

  .bg-layer {
    position: relative;
    height: 100%;
    background-color: $color-background;
    // background-color: red;
  }

  .list {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
    // right:0;
    // left:0;
    background: $color-background;
    // padding: 20px 30px;
    // overflow hidden
  }
}
.song-list-wrapper{
  padding: 20px 30px;
}
.loading-container{
  position absolute
  width 100%
  top 50%
  transform translateY(-50%)
}
</style>

